package cn.wolfcode.crm.web.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.query.SystemDictionaryItemQueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.RequiredPermission;

@Controller
@RequestMapping("/customer")
public class CustomerController {
	@Autowired
	private ICustomerService customerService;

	@Autowired
	private ISystemDictionaryItemService systemDictionaryItemService;

	@RequiredPermission({"客户列表", "customer:list"})
	@RequestMapping("/list")
	public String list(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
		PageInfo<Customer> pageInfo = customerService.list(qo);

		SystemDictionaryItemQueryObject qo1 = new SystemDictionaryItemQueryObject();
		qo1.setParentId(1L); // 职业
		List<SystemDictionaryItem> jobs = systemDictionaryItemService.listNotPage(qo1);
		qo1.setParentId(2L); // 来源
		List<SystemDictionaryItem> sources = systemDictionaryItemService.listNotPage(qo1);
		qo1.setParentId(3L); // 交流方式
		List<SystemDictionaryItem> types = systemDictionaryItemService.listNotPage(qo1);

		model.addAttribute("types", types);
		model.addAttribute("jobs", jobs);
		model.addAttribute("sources", sources);
		model.addAttribute("pageInfo", pageInfo);
		return "customer/list";
	}

	@RequiredPermission({"客户新增/更新", "customer:saveOrUpdate"})
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Customer entity) {
		customerService.saveOrUpdate(entity);
		return "redirect:/customer/list.do";
	}
}
